package cn.com.czpb.client.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import android.view.WindowManager;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import cn.com.czpb.client.app.App;
import cn.com.czpb.client.model.news.AreaOtherChannelBean;
import cn.com.czpb.client.model.usercenter.UserBean;

/**
 * Created by 猪猪侠 on 2017/6/30.
 */

public class SPUtil {
    private static SPUtil mSPutil;
    private static SharedPreferences sp;
    private static final String SETTINGS = "HZPD";
    private ACache msp;
    private UserBean user;

    private SPUtil() {
        msp = ACache.get(App.getInstance().getApplicationContext(), SETTINGS);
    }

    public static synchronized SPUtil getInstance() {
        if (mSPutil == null) {
            mSPutil = new SPUtil();
        }
        if (sp == null) {
            sp = App.getContext().getSharedPreferences("canyintoutiao", Context.MODE_PRIVATE);
        }
        return mSPutil;
    }

    public String getWelcome() {
        return msp.getAsString("welcomeString");
    }

    public void setWelcome(String obj) {
        msp.put("welcomeString", obj);
    }


    /**
     * 用于保存集合
     *
     * @param key key
     * @param map map数据
     * @return 保存结果
     */
    public  <K, V> boolean putHashMapData(String key, Map<K, V> map) {
        boolean result;
        SharedPreferences.Editor editor = sp.edit();
        try {
            Gson gson = new Gson();
            String json = gson.toJson(map);
            editor.putString(key, json);
            result = true;
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
        }
        editor.apply();
        return result;
    }

    /**
     * 用于保存集合
     *
     * @param key key
     * @return HashMap
     */
    public  <V> HashMap<String, V> getHashMapData(String key, Class<V> clsV) {
        String json = sp.getString(key, "");
        HashMap<String, V> map = new HashMap<>();
        Gson gson = new Gson();
        JsonObject obj = new JsonParser().parse(json).getAsJsonObject();
        Set<Map.Entry<String, JsonElement>> entrySet = obj.entrySet();
        for (Map.Entry<String, JsonElement> entry : entrySet) {
            String entryKey = entry.getKey();
            JsonObject value = (JsonObject) entry.getValue();
            map.put(entryKey, gson.fromJson(value, clsV));
        }
        Log.e("SharedPreferencesUtil", obj.toString());
        return map;
    }




    //当前城市
    public void setSpeed(String speed) {//设置是否提醒
        sp.edit().putString("speed", speed).commit();
    }//是否提醒

    public String getSpeed() {
        String city = sp.getString("speed", "50");
        return city;

    }

    //设置,channels
    public void setChannels(String channels) {//设置是否提醒
        sp.edit().putString("channels", channels).commit();
    }//是否提醒

    public String getChannels() {
        String channels = sp.getString("channels", "");
        return channels;

    }


    public void setTextSize(int size) {
        msp.put("textsize", size + "");
    }



    public void setWifi(String type) {
        msp.put("type", type);
    }//设置播放提醒次数

    public String getWifi() {
        return msp.getAsString("type");
    }



    public void setWifiStatus(String type) {//设置网络状态
        msp.put("nettype", type);
    }//网络类型

    public String getWifiStatus() {
        return msp.getAsString("nettype");
    }


    //定位城市
    public void setCurrentLocation(String location) {//设置是否提醒
        sp.edit().putString("location", location).commit();
    }//是否提醒

    public String getCurrentLocation() {
        String city = sp.getString("location", "0");
        return city;
    }

    //当前城市
    public void setCurrentCity(String city) {//设置是否提醒
        sp.edit().putString("city", city).commit();
    }//是否提醒

    public String getCurrentCity() {
        String city = sp.getString("city", "南宁");
        return city;
    }


    //当前城市
    public void setCurrentCityId(String id) {//设置是否提醒
        sp.edit().putString("cityid", id).commit();
    }//是否提醒

    public String getCurrentCityId() {
        String city = sp.getString("cityid", "1");
        return city;

    }
    //当前区县
    public void setCurrentCounty(AreaOtherChannelBean areaOtherChannelBean){
        if(areaOtherChannelBean == null) {
            msp.remove("country");
        }else {
            msp.put("country", areaOtherChannelBean);
        }
    }
    public AreaOtherChannelBean getCurrentCountry(){
        AreaOtherChannelBean areaOtherChannelBean = (AreaOtherChannelBean) msp.getAsObject("country");
        return areaOtherChannelBean;
    }
    //当前区县的position
    public void setCurrentCountryPosition(int position) {//设置是否提醒
        sp.edit().putInt("countryposition", position).commit();
    }
    public int getCurrentCountryPosition(){
        return sp.getInt("countryposition",-1);
    }
    public void setIsLarge(String isLarge){
        sp.edit().putString("islarge",isLarge).commit();
    }
    public String getIsLarge(){
        return sp.getString("islarge","1");
    }
    public void setIsNeedNotity(boolean type) {//设置是否提醒
        sp.edit().putBoolean("isneed", type).commit();
    }//是否提醒

    public boolean getIsNeedNotity() {
        boolean isneed = sp.getBoolean("isneed", true);
        return isneed;

    }
    public void setShowImage(String showtype){
        msp.put("showtype",showtype);
    }
    public String getShowImage(){
        return msp.getAsString("showtype");
    }

    public int getTextSize() {
        int textSize = 19;
        String ts = msp.getAsString("textsize");
        if (!TextUtils.isEmpty(ts)) {
            try {
                if (TextUtils.isDigitsOnly(ts)) {
                    textSize = Integer.parseInt(ts);
                }
            } catch (Exception e) {
            }
        }
        return textSize;
    }

    public void setUser(UserBean user) {
        this.user = user;
        String dsuser = FjsonUtil.toJsonString(user);
        if (TextUtils.isEmpty(dsuser)) {
            return;
        }
        String suser = null;
        try {
            suser = CipherUtils.encrypt(dsuser,
                    CipherUtils.getDESKey("fuckbitch".getBytes()), "DES");
        } catch (Exception e) {
            e.printStackTrace();
        }
        msp.put("user", suser);
    }

    public UserBean getUser() {
        if (null != user) {
            return user;
        }
        String dsuser = msp.getAsString("user");
        String suser = null;
        if (!TextUtils.isEmpty(dsuser)) {
            try {
                suser = CipherUtils.decrypt(dsuser,
                        CipherUtils.getDESKey("fuckbitch".getBytes()), "DES");
            } catch (Exception e) {
                e.printStackTrace();
            }
            user = FjsonUtil.parseObject(suser, UserBean.class);
        }
        return user;
    }
    public boolean getIsFirst() {
        boolean isFirst = sp.getBoolean("isFirst", true);
        return isFirst;
    }

    public void setIsFirst(boolean flag) {
        sp.edit().putBoolean("isFirst", flag).commit();
    }
    public boolean getIsNight() {
        boolean isnight = sp.getBoolean("isnight", false);
        return isnight;
    }

    public void setIsNight(boolean flag) {
        sp.edit().putBoolean("isnight", flag).commit();
    }

    public void changeAppBrightness(Activity context, boolean flag) {
        if (!flag) {// 白天

            boolean isAutoBrightness = SystemBrightManager
                    .isAutoBrightness(context);
            if (isAutoBrightness) { // 自动调整亮度
                SystemBrightManager.setBrightness(context, -1);
            } else {
                int brightValue = SystemBrightManager.getBrightness(context);
                SystemBrightManager.setBrightness(context, brightValue);
            }

        } else { // 夜间模式;//调成自动模式；
            flag = true;
            int progress = 20;
            final WindowManager.LayoutParams attrs = context.getWindow()
                    .getAttributes();
            attrs.screenBrightness = progress / 255f;
            context.getWindow().setAttributes(attrs);
        }
    }
}
